import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  BeforeInsert,
  BeforeUpdate,
  UpdateDateColumn,
  CreateDateColumn,
} from 'typeorm';
import { hashPassword } from 'src/support/untils/bcrypt';
@Entity('user')
export class User {
  @PrimaryGeneratedColumn({
    comment: '用户id',
  })
  user_id: number;

  @Column({
    comment: '用户名',
  })
  username: string;

  @Column({
    comment: '密码',
    select: false,
  })
  password: string;

  @Column({
    comment: '昵称',
    default: '',
  })
  nickname: string;

  @Column({
    comment: '头像',
    default: '',
  })
  avatar: string;

  @Column({
    comment: '手机号',
    type: 'char',
    width: 11,
    default: null,
  })
  phone: number;

  @Column({
    comment: '是否为管理员',
    type: 'boolean',
    default: false,
  })
  isAdmin: boolean;

  @Column({
    comment: '是否禁用',
    type: 'boolean',
    default: false,
  })
  isBan: boolean;

  @Column({
    comment: '创建时间',
    type: 'timestamp',
    readonly: true,
    update: false,
  })
  @CreateDateColumn()
  create_time: number;

  @Column({
    comment: '修改时间',
    type: 'timestamp',
    readonly: true,
  })
  @UpdateDateColumn()
  update_time: number;

  @BeforeInsert()
  @BeforeUpdate()
  async encryptPwd() {
    if (this.password) {
      this.password = await hashPassword(this.password);
      console.log(this.password, '----');
    }
  }
}
